package com.google.sydym6.controllers;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;

import com.google.sydym6.logic.IRadioScrobblerModel;
import com.google.sydym6.logic.RadioScrobblerModel;
import com.google.sydym6.logic.exceptions.LastfmLoginException;
import com.google.sydym6.logic.utils.Constants;
import com.google.sydym6.logic.vk.Account;
import com.google.sydym6.view.activities.LoginActivity;
import com.google.sydym6.view.activities.SettingsActivity;
import com.google.sydym6.view.fragments.LastfmLoginDialogFragment;
import com.perm.kate.api.Api;

public class SettingsController implements ISettingsController {

	private static final int REQUEST_VK_LOGIN = 1;
	IRadioScrobblerModel model;
	SettingsActivity view;

	Account vkAccount;
	Api vkApi;

	public SettingsController(SettingsActivity view) {
		this.view = view;
		this.model = RadioScrobblerModel.getInstance();

		vkAccount = new Account();
		vkAccount.restore(view);

		if (vkAccount.access_token != null) {
			vkApi = new Api(vkAccount.access_token, Constants.API_ID);
			model.getSongManager().setVkApi(vkApi);
		}
		Log.v("SettingsController", "vkApi = " + model.getSongManager().getVkApi());
	}

	@Override
	public boolean hasVkAccount() {
		return vkApi != null;
	}

	@Override
	public void changeVk() {
		if (vkApi == null) {
			view.vkSignInOutButton.setEnabled(false);
			Intent intent = new Intent();
			intent.setClass(view, LoginActivity.class);
			view.startActivityForResult(intent, REQUEST_VK_LOGIN);
		} else {
			vkApi = null;
			vkAccount.access_token = null;
			vkAccount.save(view);
			model.getSongManager().setVkApi(null);
			view.changeVkButton();
		}
	}

	@Override
	public boolean hasLastfmAccount() {
		return model.getScrobbler().hasLastFmAccount();
	}

	@Override
	public void changeLastfmAccount(FragmentManager fm, String tag) {
		if (hasLastfmAccount()) {
			try {
				model.setLastFmAccount(null, null);
			} catch (LastfmLoginException e) {
			} finally {
				view.changeLastfmButton();
			}
		} else {
			view.lastfmSignInOutButton.setEnabled(false);
			DialogFragment lastfmDialog = new LastfmLoginDialogFragment();
			lastfmDialog.show(fm, tag);
		}
	}

	@Override
	public void checkOnActivityResult(int requestCode, int resultCode,
			Intent data) {
		if (requestCode == REQUEST_VK_LOGIN) {
			if (resultCode == Activity.RESULT_OK) {
				vkAccount.access_token = data.getStringExtra("token");
				vkAccount.user_id = data.getLongExtra("user_id", 0);
				vkAccount.save(view);

				vkApi = new Api(vkAccount.access_token, Constants.API_ID);
				Log.v("SettingsController", "vkApi = " + vkApi);
				model.getSongManager().setVkApi(vkApi);
				view.changeVkButton();
				return;
			}
		}

		model.getSongManager().setVkApi(null);
		view.changeVkButton();
	}
}
